package com.happycat.interceptor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.SecurityMetadataSource;
import org.springframework.security.access.intercept.AbstractSecurityInterceptor;
import org.springframework.security.access.intercept.InterceptorStatusToken;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;

import com.happycat.manage.HappyCatAccessDecisionManager;

//@Component
public class HappyCatFilterSecurityInterceptor extends AbstractSecurityInterceptor implements Filter {

	/**
	 * 注入自定义的资源（url）权限（角色）获取类
	 */
	@Autowired
	private FilterInvocationSecurityMetadataSource happyCatFilterInvocationSecurityMetadataSource;

	/**
	 * 注入自定义的权限验证管理器
	 */
	@Autowired
	public void setAccessDecisionManager(HappyCatAccessDecisionManager happyCatAccessDecisionManager) {
		super.setAccessDecisionManager(happyCatAccessDecisionManager);
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		FilterInvocation fi = new FilterInvocation(request, response, chain);
		InterceptorStatusToken token = super.beforeInvocation(fi);
		try {
			/**
			 * 执行下一个拦截器
			 */
			HttpServletRequest req = (HttpServletRequest)request;
			System.out.println("请求链接："+req.getRequestURI());
			fi.getChain().doFilter(fi.getRequest(), fi.getResponse());
		} finally {
			super.afterInvocation(token, null);
		}

	}

	@Override
	public void destroy() {
		
	}

	@Override
	public Class<?> getSecureObjectClass() {
		return FilterInvocation.class;
	}

	@Override
	public SecurityMetadataSource obtainSecurityMetadataSource() {
		return this.happyCatFilterInvocationSecurityMetadataSource;
	}

}
